//
//  DeviceHandle.m
//  SenMo
//
//  Created by ZengYuan on 15/12/16.
//  Copyright © 2015年 ZengYuan. All rights reserved.
//

#import "DeviceHandle.h"
#import "Config.h"
#import "RequestUnit.h"
#import "RequsetManager.h"

@implementation DeviceHandle


+(void)deviceSetNewAdmi:(NSString *)device device_username:(NSString *)device_username  device_pass:(NSString *)device_pass
success:(void (^)())success failure:(void (^)(NSString *error))failure{
    
    NSString *sid = [[NSUserDefaults standardUserDefaults] objectForKey:@"session_id"];
    if(!sid)return;
    
    NSDictionary *dic = [[NSDictionary alloc] initWithObjects: @[device,sid] forKeys:@[@"device_id", @"session_id"]];
    
    NSString *json  =[dic JSONString];
    NSString *url = [NSString stringWithFormat:MAINURL,@"modify_device_user_name_and_secret.action"];
    
    [AWHttpTool post:url body:json success:^(id json) {
        
        NSString *msg = HTTPERROR_DATAFAIL;
        
        if (json) {
            
            NSInteger sta = [json[@"status"] integerValue];
            msg = json[@"msg"];
            if (sta == 0) {
                success();
                return ;
            }
        }
        
        failure(msg);
        
    } failure:^(NSError *error) {
        failure(error.domain);
    }];
    
    
    
}


+(void)deviceBind:(NSString *)device device_username:(NSString *)device_username  device_pass:(NSString *)device_pass
          success:(void (^)())success failure:(void (^)(NSString *error))failure{
    
    NSString *sid = [[NSUserDefaults standardUserDefaults] objectForKey:@"session_id"];
    if(!sid)return;
    
    NSDictionary *dic = [[NSDictionary alloc] initWithObjects:
  @[device,sid,device_username,device_pass]
             forKeys:
  @[@"device_id", @"session_id",@"device_user_name",@"device_user_secret"]];
    
    NSString *json  =[dic JSONString];
    NSString *url = [NSString stringWithFormat:MAINURL,URL_BIND];
    
    [AWHttpTool post:url body:json success:^(id json) {
        
        NSString *msg = HTTPERROR_DATAFAIL;
        
        if (json) {
            
            NSInteger sta = [json[@"status"] integerValue];
            msg = json[@"msg"];
            if (sta == 0) {
                success();
                return ;
            }
        }
        
        failure(msg);
        
    } failure:^(NSError *error) {
        failure(error.domain);
    }];
    
    
    
}

+(void)deviceBind:(NSString *)device
          result:(void (^)(NSInteger sta))result failure:(void (^)(NSString *error))failure{
    
    NSString *sid = [[NSUserDefaults standardUserDefaults] objectForKey:@"session_id"];
    if(!sid)return;
    
    NSDictionary *dic = [[NSDictionary alloc] initWithObjects: @[device,sid] forKeys:@[@"device_id", @"session_id"]];
    
    NSString *json  =[dic JSONString];
    NSString *url = [NSString stringWithFormat:MAINURL,URL_BIND];
    
    [AWHttpTool post:url body:json success:^(id json) {
        
        NSString *msg = HTTPERROR_DATAFAIL;
        
        if (json) {
            
            NSInteger sta = [json[@"status"] integerValue];
            msg = json[@"msg"];
            result(sta);
            return ;
         }
        
        failure(msg);
        
    } failure:^(NSError *error) {
        failure(error.domain);
    }];
    

    
}


+(void)deviceUnbind:(NSString *)device
            success:(void (^)())success failure:(void (^)(NSString *error))failure{
    
    NSString *sid = [[NSUserDefaults standardUserDefaults] objectForKey:@"session_id"];
    if(!sid)return;
    
    NSDictionary *dic = [[NSDictionary alloc] initWithObjects: @[device,sid] forKeys:@[@"device_id", @"session_id"]];
    
    NSString *json  =[dic JSONString];
    NSString *url = [NSString stringWithFormat:MAINURL,URL_UNBIND];
    
    [AWHttpTool post:url body:json success:^(id json) {
        
        NSString *msg = HTTPERROR_DATAFAIL;
        
        if (json) {
            
            NSInteger sta = [json[@"status"] integerValue];
            msg = json[@"msg"];
            
            if (sta == 0) {
                success();
                return ;
            }
        }
        
        failure(msg);
        
    } failure:^(NSError *error) {
        failure(error.domain);
    }];
    

    
}


+(void)deviceShare:(NSString *)device userid:(NSString *)user_id
            success:(void (^)())success failure:(void (^)(NSString *error))failure{
    
    NSString *sid = [[NSUserDefaults standardUserDefaults] objectForKey:@"session_id"];
    if(!sid)return;
    
    NSDictionary *dic = [[NSDictionary alloc] initWithObjects: @[device,sid,user_id] forKeys:@[@"device_id", @"session_id",@"user_phoneno"]];
    
    NSString *json  =[dic JSONString];
    NSString *url = [NSString stringWithFormat:MAINURL,URL_SHARE_USER];
    
    [AWHttpTool post:url body:json success:^(id json) {
        
        NSString *msg = HTTPERROR_DATAFAIL;
        
        if (json) {
            
            NSInteger sta = [json[@"status"] integerValue];
            msg = json[@"msg"];
            
            if (sta == 0) {
                success();
                return ;
            }
        }
        
        failure(msg);
        
    } failure:^(NSError *error) {
        failure(error.domain);
    }];
    
    
    
}

+(void)deviceAdminTransfer:(NSString *)device_id
                    userid:(NSString *)user_id
                   success:(void (^)())success failure:(void (^)(NSString *error))failure{
    
    NSString *sid = [[NSUserDefaults standardUserDefaults] objectForKey:@"session_id"];
    if(!sid)return;
    
    NSDictionary *dic = [[NSDictionary alloc] initWithObjects: @[device_id,sid,user_id] forKeys:@[@"device_id", @"session_id",@"user_phoneno"]];
    
    NSString *json  =[dic JSONString];
    NSString *url = [NSString stringWithFormat:MAINURL,@"manage_video_device_transfer_manage_privilege.action"];
    
    [AWHttpTool post:url body:json success:^(id json) {
        
        NSString *msg = HTTPERROR_DATAFAIL;
        
        if (json) {
            
            NSInteger sta = [json[@"status"] integerValue];
            msg = json[@"msg"];
            
            if (sta == 0) {
                success();
                return ;
            }
        }
        
        failure(msg);
        
    } failure:^(NSError *error) {
        failure(error.domain);
    }];
    
    
    
}


+(void)deviceAcceptSharerRequest:(NSString *)device_id
                          userid:(NSString *)user_id
                         success:(void (^)())success failure:(void (^)(NSString *error))failure{
    
    NSString *sid = [[NSUserDefaults standardUserDefaults] objectForKey:@"session_id"];
    if(!sid)return;
    
    NSDictionary *dic = [[NSDictionary alloc] initWithObjects: @[device_id?device_id:@"",sid,user_id] forKeys:@[@"device_id", @"session_id",@"user_phoneno"]];
    
    NSString *json  =[dic JSONString];
    NSString *url = [NSString stringWithFormat:MAINURL,@"manage_video_device_accept_share_request.action"];
    
    [AWHttpTool post:url body:json success:^(id json) {
        
        NSString *msg = HTTPERROR_DATAFAIL;
        
        if (json) {
            
            NSInteger sta = [json[@"status"] integerValue];
            msg = json[@"msg"];
            
            if (sta == 0) {
                success();
                return ;
            }
        }
        
        failure(msg);
        
    } failure:^(NSError *error) {
        failure(error.domain);
    }];
    
    
    
}


+(void)deviceunShare:(NSString *)device_id userid:(NSArray *)useridArray
             success:(void (^)())success failure:(void (^)(NSString *error))failure{
    
    NSString *sid = [[NSUserDefaults standardUserDefaults] objectForKey:@"session_id"];
    if(!sid)return;
    
    NSString *userids = [useridArray componentsJoinedByString:@","];
    
    NSDictionary *dic = [[NSDictionary alloc] initWithObjects: @[device_id,sid,userids] forKeys:@[@"device_id", @"session_id",@"user_phoneno"]];
    
    NSString *json  =[dic JSONString];
    NSString *url = [NSString stringWithFormat:MAINURL,URL_UNSHARE_USER];
    
    [AWHttpTool post:url body:json success:^(id json) {
        
        NSString *msg = HTTPERROR_DATAFAIL;
        
        if (json) {
            
            NSInteger sta = [json[@"status"] integerValue];
            msg = json[@"msg"];
            
            if (sta == 0) {
                success();
                return ;
            }
        }
        
        failure(msg);
        
    } failure:^(NSError *error) {
        failure(error.domain);
    }];
}

+(void)deviceAvailble:(NSString *)device_id
               result:(void (^)(BOOL availble))_result{
    
    NSString *sid = [[NSUserDefaults standardUserDefaults] objectForKey:@"session_id"];
    if(!sid)return;
    
    
    NSDictionary *dic = [[NSDictionary alloc] initWithObjects: @[sid,device_id] forKeys:@[@"session_id",@"device_id"]];
    
    NSString *json  =[dic JSONString];
    NSString *url = [NSString stringWithFormat:MAINURL,URL_NORMAL_VIDEO_LOOK];
    
    [AWHttpTool post:url body:json success:^(id json) {
        
        NSString *msg = HTTPERROR_DATAFAIL;
        
        if (json) {
            
            NSInteger sta = [json[@"status"] integerValue];
            msg = json[@"msg"];
            
            if (sta == 0) {
                _result(YES);
                return ;
            }
        }
        
        _result(NO);
        
    } failure:^(NSError *error) {
        _result(NO);
    }];

}

+(void)deviceSetTimeInterval:(NSString *)device_id
                  start_time:(NSString *)start_time
                    end_time:(NSString *)end_time
                     success:(void (^)())success failure:(void (^)(NSString *error))failure{
    
    NSString *sid = [[NSUserDefaults standardUserDefaults] objectForKey:@"session_id"];
    if(!sid)return;
    
    
    NSDictionary *dic = [[NSDictionary alloc] initWithObjects: @[sid,device_id,start_time,end_time] forKeys:@[@"session_id",@"device_id",@"start_time",@"end_time"]];
    
    NSString *json  =[dic JSONString];
    NSString *url = [NSString stringWithFormat:MAINURL,@"manage_video_device_set_share_timeinterval.action"];
    
    [AWHttpTool post:url body:json success:^(id json) {
        
        NSString *msg = HTTPERROR_DATAFAIL;
        
        if (json) {
            
            NSInteger sta = [json[@"status"] integerValue];
            msg = json[@"msg"];
            
            if (sta == 0) {
                success();
                return ;
            }
        }
        
        failure(msg);
    } failure:^(NSError *error) {
        failure(HTTPERROR_DATAFAIL);
    }];
    
}


+(void)deviceGetTimeInterval:(NSString *)device_id
                     success:(void (^)(NSString *start_time, NSString *end_time))success
                     failure:(void (^)(NSString *error))failure{
 
    
    NSString *sid = [[NSUserDefaults standardUserDefaults] objectForKey:@"session_id"];
    if(!sid)return;
    
    
    NSDictionary *dic = [[NSDictionary alloc] initWithObjects:
                            @[sid,device_id]
                    forKeys:@[@"session_id",@"device_id"]];
    
    NSString *json  =[dic JSONString];
    NSString *url = [NSString stringWithFormat:MAINURL,@"manage_video_device_fetch_share_timeinterval.action"];
    
    [AWHttpTool post:url body:json success:^(id json) {
        
        NSString *msg = HTTPERROR_DATAFAIL;
        
        if (json) {
            
            NSInteger sta = [json[@"status"] integerValue];
            msg = json[@"msg"];
            
            if (sta == 0) {
                NSString *start_time = json[@"start_time"];
                NSString *end_time = json[@"end_time"];

                success(start_time, end_time);
                return ;
            }
        }
        
        failure(msg);
    } failure:^(NSError *error) {
        failure(HTTPERROR_DATAFAIL);
    }];
}

+(void)deviceGetAllRequest:(NSString *)device_id
                   success:(void (^)())success
                   failure:(void (^)(NSString *error))failure{
    
    NSString *sid = [[NSUserDefaults standardUserDefaults] objectForKey:@"session_id"];
    if(!sid)return;

    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"session_id"]=sid;
    if (device_id.length>0) {
        dic[@"device_id"]=device_id;
    }
    
//    NSDictionary *dic = [[NSDictionary alloc] initWithObjects:
//                         @[sid,device_id?device_id:@""]
//                            forKeys:@[@"session_id",@"device_id"]];
    
    NSString *json  =[dic JSONString];
    NSString *url = [NSString stringWithFormat:MAINURL,URL_GETREQUESTINFO];
    
    [AWHttpTool post:url body:json success:^(id json) {
        
        NSString *msg = HTTPERROR_DATAFAIL;
        
        if (json) {
            
            NSLog(@"json=%@",json);
            
            NSInteger sta = [json[@"status"] integerValue];
            msg = json[@"msg"];
            
            if (sta == 0) {
      
                NSMutableArray *result = [NSMutableArray array];
                
                NSArray *messages = json[@"messages"];
                for (NSDictionary *dic in messages) {
                    
                    RequestUnit *request = [[RequestUnit alloc]init];
                    request.requester_phoneno = dic[@"requester_phoneno"];
                    request.device_id = dic[@"device_id"];
                    request.create_time = dic[@"request_timestamp"];
                    request.deviceName = dic[@"device_name"];
                    request.requester_name = dic[@"requester_name"];

                    [result addObject:request];
                }
                
////                for (NSDictionary *dic in messages) {
//                
//                    RequestUnit *request = [[RequestUnit alloc]init];
//                    request.requester_phoneno = @"12345678921";
//                    request.device_id = @"1111111111112006";
//                    
//                    [result addObject:request];
////                }
 

                
                [RequsetManager addNewRequest:device_id?device_id:@"" request:result];
                
                success();
                return ;
            }
        }
        
        failure(msg);
    } failure:^(NSError *error) {
        failure(HTTPERROR_DATAFAIL);
    }];
}

+(void)deviceChangeName:(NSString *)newName deviceId:(NSString *)device_id
                success:(void (^)())success
                failure:(void (^)(NSString *error))failure{
    
    NSString *sid = [[NSUserDefaults standardUserDefaults] objectForKey:@"session_id"];
    if(!sid)return;
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"session_id"]=sid;
    dic[@"device_id"]=device_id;
    dic[@"device_name"]=newName;

    
    NSString *json  =[dic JSONString];
    NSString *url = [NSString stringWithFormat:MAINURL,URL_CHANGEDEVICENAME];
    
    [AWHttpTool post:url body:json success:^(id json) {
        
        NSString *msg = HTTPERROR_DATAFAIL;
        
        if (json) {
            
            NSInteger sta = [json[@"status"] integerValue];
            msg = json[@"msg"];
            
            if (sta == 0) {
                
                success();
                return ;
            }
        }
        
        failure(msg);
    } failure:^(NSError *error) {
        failure(HTTPERROR_DATAFAIL);
    }];
    
}

+(void)channelChangeName:(NSString *)newName deviceId:(NSString *)device_id
       device_channel_no:(NSInteger)device_channel_no
                success:(void (^)())success
                failure:(void (^)(NSString *error))failure{
    
    NSString *sid = [[NSUserDefaults standardUserDefaults] objectForKey:@"session_id"];
    if(!sid)return;
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"session_id"]=sid;
    dic[@"device_id"]=device_id;
    dic[@"device_channel_no"]=@(device_channel_no);
    dic[@"device_channel_name"]=newName;
    
    
    NSString *json  =[dic JSONString];
    NSString *url = [NSString stringWithFormat:MAINURL,URL_CHANGECHANNELNAME];
    
    [AWHttpTool post:url body:json success:^(id json) {
        
        NSString *msg = HTTPERROR_DATAFAIL;
        
        if (json) {
            
            NSInteger sta = [json[@"status"] integerValue];
            msg = json[@"msg"];
            
            if (sta == 0) {
                
                success();
                return ;
            }
        }
        
        failure(msg);
    } failure:^(NSError *error) {
        failure(HTTPERROR_DATAFAIL);
    }];
    
}

 
@end
